package com.alliky.core.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.alliky.core.R;
import com.alliky.core.util.DimenUtil;
import com.alliky.core.util.KLog;
import com.alliky.core.util.StringUtils;

/**
 * ================================================
 *
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2022/8/19 16:34
 * ================================================
 */
public class RxDialog {

    private static Context mContext;
    private Dialog mDialog = null;

    public RxDialog(final Builder builder) {
        if (isValidContext(mContext)) {
            mDialog = new Dialog(mContext, R.style.custom_dialog);
            View convertView = LayoutInflater.from(mContext).inflate(R.layout.dialog_common_confirm_cancel, null);
            TextView tvTitle = convertView.findViewById(R.id.tv_title);
            TextView tvContent = convertView.findViewById(R.id.tv_content);
            TextView tvCancel = convertView.findViewById(R.id.tv_cancel);
            TextView tvConfirm = convertView.findViewById(R.id.tv_confirm);
            View line1 = convertView.findViewById(R.id.line1);

            if (StringUtils.isNotBlank(builder.mTitle)) {
                tvTitle.setText(builder.mTitle);
            }

            if (!builder.mTitleVisible) {
                tvTitle.setVisibility(View.GONE);
            }

            if (StringUtils.isNotBlank(builder.mContent)) {
                tvContent.setText(builder.mContent);
            }

            if (StringUtils.isNotBlank(builder.mNegativeText)) {
                tvCancel.setText(builder.mNegativeText);
            }

            if (StringUtils.isNotBlank(builder.mPositiveText)) {
                tvConfirm.setText(builder.mPositiveText);
            }

            if (!builder.mNegativeVisible) {
                line1.setVisibility(View.GONE);
                tvCancel.setVisibility(View.GONE);
            }

            tvCancel.setTextColor(ContextCompat.getColor(mContext, builder.mNegativeTextColor));
            tvConfirm.setTextColor(ContextCompat.getColor(mContext, builder.mPositiveTextColor));

            mDialog.setCancelable(builder.mCancelable);

            mDialog.setContentView(convertView);

            Window dialogWindow = mDialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();

            if (builder.mWidth != 0) {
                lp.width = (int) (DimenUtil.getScreenWidth() * builder.mWidth);
            } else {
                lp.width = (int) (DimenUtil.getScreenWidth() * 0.73);
            }
            dialogWindow.setAttributes(lp);

            tvCancel.setOnClickListener(v -> {
                if (builder.mNegativeListener != null) {
                    builder.mNegativeListener.onClick(mDialog);
                }
                dismiss();
            });

            tvConfirm.setOnClickListener(v -> {
                if (builder.mPositiveListener != null) {
                    builder.mPositiveListener.onClick(mDialog);
                }
            });
        }
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public static class Builder {

        private String mTitle;
        private String mContent;
        private String mPositiveText;
        private String mNegativeText;
        private double mWidth = 0;
        private double mHeight = 0;
        private boolean mNegativeVisible = true;
        private boolean mTitleVisible = true;
        private boolean mCancelable = true;
        private int mPositiveTextColor = R.color.text_black;
        private int mNegativeTextColor = R.color.text_darkgray;

        private OnNegativeListener mNegativeListener;
        private OnPositiveListener mPositiveListener;

        private int mGravity = Gravity.CENTER;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder whitTitle(String title) {
            mTitle = title;
            return this;
        }

        public Builder whitContent(String content) {
            mContent = content;
            return this;
        }

        public Builder whitPositiveText(String positiveText) {
            this.mPositiveText = positiveText;
            return this;
        }

        public Builder whitNegativeText(String negativeText) {
            this.mNegativeText = negativeText;
            return this;
        }

        public Builder whitNegativeTextColor(int negativeTextColor) {
            this.mNegativeTextColor = negativeTextColor;
            return this;
        }

        public Builder whitPositiveTextColor(int positiveTextColor) {
            this.mPositiveTextColor = positiveTextColor;
            return this;
        }

        public Builder whitTitleVisiable(boolean visiable) {
            this.mTitleVisible = visiable;
            return this;
        }

        public Builder whitCancelable(boolean isCancelable) {
            this.mCancelable = isCancelable;
            return this;
        }

        public Builder whitWidth(double width) {
            this.mWidth = width;
            return this;
        }

        public Builder whitHeight(double height) {
            this.mHeight = height;
            return this;
        }

        public Builder whitGravity(int gravity) {
            this.mGravity = gravity;
            return this;
        }

        public Builder setNegativeListener(OnNegativeListener listener) {
            this.mNegativeListener = listener;
            return this;
        }

        public Builder setPositiveListener(OnPositiveListener listener) {
            this.mPositiveListener = listener;
            return this;
        }

        public RxDialog build() {
            return new RxDialog(this);
        }
    }

    public interface OnNegativeListener {
        void onClick(Dialog dialog);
    }

    public interface OnPositiveListener {
        void onClick(Dialog dialog);
    }

    private boolean isValidContext(Context context) {
        Activity activity = (Activity) context;
        if (activity.isDestroyed() || activity.isFinishing()) {
            KLog.i("YXH", "Activity is invalid." + " isDestoryed-->" + activity.isDestroyed() + " isFinishing-->" + activity.isFinishing());
            return false;
        } else {
            return true;
        }
    }
}
